package com.yc.utilslib.reader.card;


import android.os.Parcel;
import android.os.Parcelable;

import com.yc.utilslib.utils.ByteUtils;
import com.yc.utilslib.utils.DateUtils;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 作者：TanTun
 * 时间：2017/2/26
 * 邮箱：32965926@qq.com
 * 描述：卡上余额数据块实体类
 */

public class ValueInfo implements Parcelable {
    private byte[] mBlockData;
    private BigDecimal mCardValue;
    private BigDecimal mLastPayValue;
    private Date mLastPayDate;
    private int mPayCount;
    private BigDecimal mDayPayValue;

    /**
     * 余额块信息构造函数
     *
     * @param data 余额块的数据
     */
    private ValueInfo(byte[] data) {
        this.mBlockData = data;
        mCardValue = new BigDecimal(ByteUtils.byteArrayToInt(data, 0, 3, ByteUtils.Endian.Big)
                / 100.00);
        mLastPayValue = new BigDecimal(ByteUtils.byteArrayToInt(data, 3, 3, ByteUtils.Endian.Big)
                / 100.00);

        try {
            mLastPayDate = DateUtils.YYYYMMDDHHMMSS_SHORT.parse(ByteUtils
                    .byteArrayToBcdString(DateUtils.convertConsumeDateBytes(data, 6)) + ByteUtils
                    .byteArrayToBcdString(DateUtils.convertConsumeTimeBytes(data, 9)));
        } catch (java.text.ParseException e) {
            e.printStackTrace();
            mLastPayDate = new Date(System.currentTimeMillis());
        }

        mPayCount = ByteUtils.byteArrayToInt(data, 11, 2, ByteUtils.Endian.Big);
        mDayPayValue = new BigDecimal(ByteUtils.byteArrayToInt(data, 13, 2, ByteUtils.Endian.Big)
                / 100.00);
    }

    protected ValueInfo(Parcel in) {
        mBlockData = in.createByteArray();
        mCardValue = (BigDecimal) in.readSerializable();
        mLastPayValue = (BigDecimal) in.readSerializable();
        mLastPayDate = (Date) in.readSerializable();
        mPayCount = in.readInt();
        mDayPayValue = (BigDecimal) in.readSerializable();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByteArray(mBlockData);
        dest.writeSerializable(mCardValue);
        dest.writeSerializable(mLastPayValue);
        dest.writeSerializable(mLastPayDate);
        dest.writeInt(mPayCount);
        dest.writeSerializable(mDayPayValue);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<ValueInfo> CREATOR = new Creator<ValueInfo>() {
        @Override
        public ValueInfo createFromParcel(Parcel in) {
            return new ValueInfo(in);
        }

        @Override
        public ValueInfo[] newArray(int size) {
            return new ValueInfo[size];
        }
    };

    /**
     * 根据传入的数据构造一个新的实例
     *
     * @param data 余额块的数据
     * @return 余额信息
     */
    static ValueInfo getInstance(byte[] data) {
        return new ValueInfo(data);
    }

    /**
     * @return 卡上余额
     */
    public BigDecimal getCardValue() {
        return mCardValue;
    }

    /**
     * @return 上次交易金额
     */
    public BigDecimal getLastPayValue() {
        return mLastPayValue;
    }

    /**
     * @return 上次交易时间
     */
    public Date getLastPayDate() {
        return mLastPayDate;
    }

    /**
     * @return 交易总次数
     */
    public int getPayCount() {
        return mPayCount;
    }

    /**
     * @return 当天交易总额
     */
    public BigDecimal getDayPayValue() {
        return mDayPayValue;
    }


    /**
     * @param cardValue 卡上余额
     */
    public void setCardValue(BigDecimal cardValue) {
        mCardValue = cardValue;
    }

    /**
     * @param lastPayValue 最后交易额
     */
    public void setLastPayValue(BigDecimal lastPayValue) {
        mLastPayValue = lastPayValue;
    }

    /**
     * @param lastPayDate 最后交易时间
     */
    public void setLastPayDate(Date lastPayDate) {
        mLastPayDate = lastPayDate;
    }

    /**
     * @param payCount 交易总次数
     */
    public void setPayCount(int payCount) {
        mPayCount = payCount;
    }

    /**
     * @param dayPayValue 当天交易总额
     */
    public void setDayPayValue(BigDecimal dayPayValue) {
        mDayPayValue = dayPayValue;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(50);
        sb.append("卡片余额：")
                .append(mCardValue.setScale(2, BigDecimal.ROUND_DOWN).toString())
                .append(System.getProperty("line.separator"));
        sb.append("上笔交易：")
                .append(mLastPayValue.setScale(2, BigDecimal.ROUND_DOWN).toString())
                .append(System.getProperty("line.separator"));
        sb.append("交易日期：").append(DateUtils.YYYYMMDDHHMMSS_LONG.format(mLastPayDate))
                .append(System.getProperty("line.separator"));
        sb.append("交易次数：").append(mPayCount).append(System.getProperty("line.separator"));
        sb.append("当日总额：").append(mDayPayValue.setScale(2, BigDecimal.ROUND_DOWN).toString());
        return sb.toString();
    }
}
